package be.gnx.fogo.application.model;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.hibernate.Transaction;

import be.gnx.fogo.application.dao.LanguageDao;
import be.gnx.fogo.application.util.HibernateUtil;

public class Language {

  private ISO_639_1 iso_639_1;

  private String label;

  public static void initialise() {
    /* data coming from
     *          http://en.wikipedia.org/wiki/ISO_639
     */
    LanguageDao languageDao = new LanguageDao();
    Transaction transaction = HibernateUtil.beginTransaction();
    List<Language> languages = languageDao.getAll();
    HashMap<ISO_639_1, Language> languagesHashMap = new HashMap<ISO_639_1, Language>();
    for (Language language : languages) {
      languagesHashMap.put(language.iso_639_1, language);
    }

    final List<Language> initialLanguages = new ArrayList<Language>(); {
      //initialLanguages.add(new Language(new Label("zz__unknown__language", "unknown"), ISO_639_1.ZZ));
      initialLanguages.add(new Language(ISO_639_1.ZZ, "zz__unknown__language"));
      
    }
    for (Language language : initialLanguages) {
      if (!languagesHashMap.containsKey(language.iso_639_1)) {
        languageDao.save(language);
      }
    }
    transaction.commit();
  }

  public Language() {
    
  }

  private Language(ISO_639_1 iso_639_1, String label) {
    this.iso_639_1 = iso_639_1;
    this.label = label;
  }

  public String getLabel() {
    return label;
  }

  public void setLabel(String label) {
    this.label = label;
  }

  public ISO_639_1 getIso_639_1() {
    return iso_639_1;
  }

  public void setIso_639_1(ISO_639_1 iso_639_1) {
    this.iso_639_1 = iso_639_1;
  }

  public String getIso_639_1_as_string() {
    return iso_639_1.getLanguage();
  }

  public void setIso_639_1_as_string(String iso_639_1) {
    this.iso_639_1 = ISO_639_1.typeOf(iso_639_1);
  }

}
